package gr.anagnosg.general.utils;

import java.awt.Desktop;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.List;

public class ExtrernalUtils {
    public ExtrernalUtils() {
        super();
    }
    
    
    
    public static boolean  runBatCommand(String batFullName){
        boolean output = false;
        int exitVal = 2;
        String[] cmd = new String[3];
        try
                {            
                    
                    cmd[0] = "cmd.exe" ;
                    cmd[1] = "/C" ;
                    cmd[2] = batFullName;
                   
                    
                    Runtime rt = Runtime.getRuntime();
                    System.out.println("Execing " + cmd[0] + " " + cmd[1] + " " + cmd[2]);
                    Process proc = rt.exec(cmd);
                    
                                            
                    // any error???
                    exitVal = proc.waitFor();
                    if(exitVal==0){
                        output=true;
                    }
                    System.out.println("ExitValue: " + exitVal);        
                } catch (Throwable t){
                    t.printStackTrace();
                
                }
                
              return output;  
        
    }
    
    
    public static void checkSourceDirMap(String filedirSource, String batFullName)throws Exception {
        File f = new File(filedirSource);
        if(!f.exists()){
            boolean output = runBatCommand(batFullName);
            if(!output){
                throw new Exception("Mapdrive command failed!");
            }
        }
    } 
    
    
   public static String executeCommand(String command) {
                System.out.println(command);
		StringBuffer output = new StringBuffer();
 
		Process p;
		try {
			p = Runtime.getRuntime().exec(command);
			p.waitFor();
			BufferedReader reader = 
                            new BufferedReader(new InputStreamReader(p.getInputStream()));
 
                        String line = "";			
			while ((line = reader.readLine())!= null) {
				output.append(line + "\n");
			}
 
		} catch (Exception e) {
			e.printStackTrace();
		}
 
		return output.toString();
 
	}
    
    
    
    public static String executeCommandBuilder(List<String> args) throws IOException, InterruptedException{
    
        String output = "";
        ProcessBuilder pb = new ProcessBuilder (args);
       try{
        Process p = pb.start();
        int i = p.waitFor();
        
        //System.out.println("Builder output:"+i);
        /*InputStream is = p.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String line;
        while ((line = br.readLine()) != null) {
          System.out.println(line);
          output+=line+"\n";
        }*/
        /*OutputStream outs = p.getOutputStream();
        
        BufferedReader brOut = new BufferedReader(outs);
        String lineOut;
        while ((lineOut = brOut.readLine()) != null) {
          System.out.println(lineOut);
          output+=lineOut+"\n";
        }*/
        System.out.println(ListUtils.concatListElements(args, " "));
        InputStream errors = p.getErrorStream();
        InputStreamReader errorr = new InputStreamReader(errors);
        BufferedReader brError = new BufferedReader(errorr);
        String lineError;
        while ((lineError = brError.readLine()) != null) {
          System.out.println(lineError);
          output+=lineError+"\n";
        }
        
        //System.out.println("Program terminated!");
       }
       catch(Exception ex){
           ex.printStackTrace();
       }
        return  output;
    }
    public static void openDirWithDefaultOSFileExplorer(String dir){
        Desktop desktop = null;
        // on Windows, retrieve the path of the "Program Files" folder
        File file = new File(dir);
        if (Desktop.isDesktopSupported()) {
          desktop = Desktop.getDesktop();
        }
        try {
          desktop.open(file);
        }
        catch (IOException e){  e.printStackTrace();}
            
    }
    
    public static void main(String[] args) {
 
		
 
		//String domainName = "google.com";
 
		//in mac oxs
		//String command = "ping -c 3 " + domainName;
 
		//in windows
		//String command = "ping -n 3 " + domainName;
 
		//String output = ExtrernalUtils.executeCommand(command);
 
		//System.out.println(output);
                
 
    }
    
}
